package com.mlethe.android.demo.gesturelock;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.android.demo.R;
import com.mlethe.library.widget.gesturelock.GestureLockViewGroup;
import com.mlethe.library.widget.gesturelock.listener.GestureEventListener;
import com.mlethe.library.widget.gesturelock.listener.GesturePasswordSettingListener;
import com.mlethe.library.widget.gesturelock.listener.GestureUnmatchedExceedListener;

import static com.mlethe.android.demo.gesturelock.GestureActivity.PWD_MODE;

public class MainGestureActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int GESTURE_VERIFY_REQUEST_CODE = 0x00012;

    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_main);
        findViewById(R.id.pwdTv).setOnClickListener(this);
        findViewById(R.id.pwdVerifyTv).setOnClickListener(this);
        GestureLockViewGroup gestureLockView = findViewById(R.id.gestureLockView);
        sp = getSharedPreferences("GESTURE", Context.MODE_PRIVATE);
        gestureLockView.setPassword("123");
        gestureLockView.setGestureEventListener(new GestureEventListener() {
            @Override
            public void onVerify(boolean status, int tryTimes) {
                Log.e("TAG", "onVerify: " + status);
            }
        });
        gestureLockView.setGesturePasswordSettingListener(new GesturePasswordSettingListener() {
            @Override
            public void onFirstInputComplete(String text) {
                Log.e("TAG", "onFirstInputComplete: " + text);
            }

            @Override
            public void onSuccess(String text) {
                Log.e("TAG", "onSuccess: " + text);
            }

            @Override
            public void onFail(String text) {
                Log.e("TAG", "onFail: " + text);
            }
        });
        gestureLockView.setGestureUnmatchedExceedListener(2, new GestureUnmatchedExceedListener() {
            @Override
            public void onUnmatchedExceedBoundary() {
                Log.e("TAG", "onUnmatchedExceedBoundary: ");
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.pwdTv) {
            String pwd = sp.getString(PWD_MODE, "");
            Intent intent = new Intent(this, GestureActivity.class);
            if (TextUtils.isEmpty(pwd)) {
                intent.putExtra(PWD_MODE, GestureActivity.PWD_MODE_UPDATE);
            } else {
                intent.putExtra(PWD_MODE, GestureActivity.PWD_MODE_SETTING);
            }
            startActivity(intent);
        } else if (id == R.id.pwdVerifyTv) {
            Intent intent = new Intent(this, GestureActivity.class);
            intent.putExtra(PWD_MODE, GestureActivity.PWD_MODE_VERIFY);
            startActivityForResult(intent, GESTURE_VERIFY_REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GESTURE_VERIFY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, "验证通过", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "验证失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
